/* ******************************************************************
 * This file is part of the escal project and distributed under the
 * terms of the GNU General Public License Version 3
 * Copyright (C) 2008 Epocher (xijiaosdq@gmail.com)
 * See COPYING file for licensing information.
 *******************************************************************/

#include "escal.h"
#include "escal_settings.h"
#include <url_dialog.h>
#include <QFileDialog>
#include <QClipboard>
#include <QFileDialog>


using namespace Escal;
UrlDialog::UrlDialog(): QDialog()
{      
        setupUi(this);
        dirDialog = new QFileDialog(this);
        dirDialog->setFileMode(QFileDialog::DirectoryOnly);
	dirDialog->setAcceptMode(QFileDialog::AcceptOpen);
	//dirDialog->setViewMode(QFileDialog::ShowDirsOnly);
	folderInput->addItems(settings->value("Folder/DownloadFolderList")
	    .toStringList());
	folderInput->setCurrentIndex(0);
	connect(browseButton, SIGNAL(clicked()),
	    this, SLOT(browseDir()));
	connect(this, SIGNAL(accepted()),
	    this, SLOT(accepted()));
	connect(urlInput, SIGNAL(textChanged ( const QString &)),
	    this, SLOT(updateFileName(const QString &)));
}

UrlDialog::~UrlDialog(){
    

}

QUrl UrlDialog::getUrl(){
	return urlInput->text();
}

void UrlDialog::setUrlStr(const QString &urlStr){
	return urlInput->setText(urlStr);
}

QString UrlDialog::getFileName(){
	return fileNameInput->text();
}

QString UrlDialog::getFolderName(){
	return folderInput->currentText();
}

int UrlDialog::getThreadNum(){
        return threadNum->value();
}

void UrlDialog::reset(){
	urlInput->clear();
	fileNameInput->clear();
	folderInput->clear();
        folderInput->addItems(settings->value("Folder/DownloadFolderList")
	    .toStringList());
	threadNum->setValue(settings->value("eGear/DefaultThreadNum").toInt());
	QClipboard *clipboard = QApplication::clipboard();
	QString clipText = clipboard->text();
        if(clipText.startsWith("http://", Qt::CaseInsensitive) ||
	    clipText.startsWith("https://", Qt::CaseInsensitive) ||
            clipText.startsWith("ftp://", Qt::CaseInsensitive))
	{
		QUrl url = QUrl::fromEncoded(clipText.toUtf8());
	    	urlInput->setText(url.toString());
		fileNameInput->setFocus(Qt::MouseFocusReason);

	}
	else{
		urlInput->setFocus(Qt::MouseFocusReason);
	}

}

void UrlDialog::updateFileName(const QString &urlString){
    fileNameInput->setText(urlString.section('/', -1));

}

void UrlDialog::browseDir(){
    if(dirDialog->exec()){
        QString folderName=dirDialog->selectedFiles()[0];
	folderInput->lineEdit()->setText(folderName);
    }

}

void UrlDialog::accepted(){
    QString folderName=folderInput->lineEdit()->text();
    while(folderName.endsWith("/")){
        folderName.resize(folderName.size()-1);
    }
    if(folderName=="") folderName = "/";
    QStringList folderList = settings->value("Folder/DownloadFolderList")
        .toStringList();
    if(folderList.contains(folderName)){
        folderList.removeAll(folderName);
    }
    folderList.insert(0, folderName);
    settings->setValue("Folder/DownloadFolderList", folderList);
    folderInput->clear();
    folderInput->addItems(folderList);
    settings->setValue("eGear/DefaultThreadNum", threadNum->value());
}
